
MyQuests.ObjectiveTypes = {
  --["None"]       = 1,
  ["Emote"]      = 2,
  ["Explore"]    = 3,
  ["DuelAny"]    = 4,
  ["DuelClass"]  = 5,
  ["DuelGuild"]  = 6,
  ["DuelPlayer"] = 7,
  ["DuelRace"]   = 8,
  ["GatherItem"] = 9,
  ["KillMonster"] = 10,
  ["PvpAny"]     = 11,
  ["PvpClass"]   = 12,
  ["PvpGuild"]   = 13,
  ["PvpPlayer"]  = 14,
  ["PvpRace"]    = 15,
  ["Tame"]       = 16      
};

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:PrintObjectiveUpdate(strIn)
  UIErrorsFrame:AddMessage(self.message, 1.0, 1.0, 0.0, 5.0);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
function MyQuests:CheckQuestComplete(questIn)
  if (not questIn.isComplete and not questIn.isFailed) then
    questIn.isComplete = 1;
    
    local objCount = table.getn(questIn.objectives);
    for i=1, objCount, 1 do
      local tmpObj = questIn.objectives[i];
      
      if (not tmpObj.isComplete) then
        questIn.isComplete = nil;
        break;
      end
    end

    -- TODO: fire QUEST_LOG_UPDATE event
  end
end


--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives = {};

function MyQuests.QuestObjectives:GetLeaderBoard(objective, strIn)
  local strOut = strIn;

  -- replace %n with the "Name" value
  if (objective.name) then
    strOut = string.gsub(strOut, "%%n", objective.name);
  end

  if (objective.location) then
    strOut = string.gsub(strOut, "%%l", objective.location);
  end
  
  -- replace %f with the proper faction string
  --local faction = "Alliance";
  --if (UnitFactionGroup("player") == faction) then
  --  faction = "Horde";
  --end
  --strOut = string.gsub(strOut, "%%f", faction);  

  -- replace %r with the proper race
  
  -- replace %c with the proper class
  

  -- add the count tracking to the end, if needed
  if (objective.total) then
    strOut = string.gsub(strOut, "%%i", objective.count);
    strOut = string.gsub(strOut, "%%c", objective.total);
  end  
  
  return strOut;
end

function MyQuests.QuestObjectives.SetEditor(objIn, objEditor)
  -- update drop down
  -- fill in all text fields
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.Explore = {};
function MyQuests.QuestObjectives.Explore:OnCreateOrEdit(refStr, objOut)
  --local objOut = {};

  objOut.location = getglobal(refStr .. "LocationEntry"):GetText();

  if (objOut.location ~= nil and objOut.location ~= "") then
    objOut.type = "Explore";

    if (objOut.name ~= nil and objOut.name ~= "") then
      objOut.name = getglobal(refStr .. "NameEntry"):GetText();
    end

    objOut.leaderBoard = getglobal(refStr .. "ShowLeader"):GetChecked();
    objOut.coordX = tonumber(getglobal(refStr .. "XCoord"):GetText());
    objOut.coordY = tonumber(getglobal(refStr .. "YCoord"):GetText());
    
    return 1;
  end
  
  return nil;
end
function MyQuests.QuestObjectives.Explore:OnUpdate(objIn)
  if (not objIn.isComplete) then
    if (GetZoneText() == objIn.location or GetSubZoneText() == objIn.location) then
      if (objIn.coordX and objIn.coordY) then
        local px, py = GetPlayerMapPosition("player");
        px = math.floor(px * 100);
        py = math.floor(py * 100);
        
        if (objIn.coordX == px and objIn.coordY == py) then
          objIn.isComplete = 1;
          -- TODO: print out a progress
          --mq.IO.banner(mq.QuestObjectives.GetLeaderBoard(objective, MQ_MSG_QUESTUPDATE_MONSTER));
          return 1;
        end
      else
        objIn.isComplete = 1;
        -- TODO: print out a progress
        --mq.IO.banner(mq.QuestObjectives.GetLeaderBoard(objective, MQ_MSG_QUESTUPDATE_MONSTER));
        return 1;        
      end
    end
  end
  
  return nil;
end
function MyQuests.QuestObjectives.Explore:GetLeaderBoard(objective)
  if (objective.name) then
    return MyQuests.QuestObjectives:GetLeaderBoard(objective, objective.name);
  end

  return MyQuests.QuestObjectives:GetLeaderBoard(objective, MQL["ExploreLeaderBoard"]);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.Duel = {};
function MyQuests.QuestObjectives.Duel:OnCreateOrEdit(refStr, objOut)
  --local objOut = {};

  objOut.leaderBoard = getglobal(refStr .. "ShowLeader"):GetChecked();

  objOut.location = getglobal(refStr .. "LocationEntry"):GetText();
  if (objOut.location ~= nil and objOut.location ~= "") then
    objOut.coordX = tonumber(getglobal(refStr .. "XCoord"):GetText());
    objOut.coordY = tonumber(getglobal(refStr .. "YCoord"):GetText());
  end

  objOut.total = tonumber(getglobal(refStr .. "CountEntry"):GetText());
  if (not objOut.total or objOut.total == 0) then
    objOut.total = 1;
  end
  objOut.count = 0;
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.DuelAny = {};
function MyQuests.QuestObjectives.DuelAny:OnCreateOrEdit(refStr, objOut)
  --local objOut = {};

  objOut.type = "DuelAny";
  --objOut.name = getglobal(refStr .. "NameEntry"):GetText();

  objOut.minLevel = tonumber(getglobal(refStr .. "MinLevel"):GetText());
  objOut.maxLevel = tonumber(getglobal(refStr .. "MaxLevel"):GetText());

  MyQuests.QuestObjectives.Duel:OnCreateOrEdit(refStr, objOut);

  return 1;
end
function MyQuests.QuestObjectives.DuelAny:OnUpdate(refObj, targetInfo)
  if (refObj.count < refObj.total) then
    if ((refObj.minLevel and targetInfo.level < refObj.minLevel) or
        (refObj.maxLevel and targetInfo.level > refObj.maxLevel)) then
      return nil;
    end

    refObj.count = refObj.count + 1;
    if (refObj.count >= refObj.total) then
      refObj.isComplete = 1;
    end
    -- TODO: print out a progress
    --mq.IO.banner(mq.QuestObjectives.GetLeaderBoard(objective, MQ_MSG_QUESTUPDATE_MONSTER));
  end
end
function MyQuests.QuestObjectives.DuelAny:GetLeaderBoard(objective)
  return MyQuests.QuestObjectives:GetLeaderBoard(objective, MQL["DuelAnyLeaderBoard"]);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.DuelClass = {};
function MyQuests.QuestObjectives.DuelClass:OnCreateOrEdit(refStr, objOut)
  --objOut = {};

  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "DuelClass";
    MyQuests.QuestObjectives.Duel:OnCreateOrEdit(refStr, objOut);

    objOut.minLevel = tonumber(getglobal(refStr .. "MinLevel"):GetText());
    objOut.maxLevel = tonumber(getglobal(refStr .. "MaxLevel"):GetText());

    return 1;
  end

  return nil;
end
function MyQuests.QuestObjectives.DuelClass:OnUpdate(refObj, targetInfo)
  if (targetInfo.class == refObj.name) then
    MyQuests.QuestObjectives["DuelAny"]:OnUpdate(refObj, targetInfo);
  end
end
function MyQuests.QuestObjectives.DuelClass:GetLeaderBoard(objective)
  return MyQuests.QuestObjectives:GetLeaderBoard(objective, MQL["DuelClassLeaderBoard"]);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.DuelGuild = {};
function MyQuests.QuestObjectives.DuelGuild:OnCreateOrEdit(refStr, objOut)
  --objOut = {};

  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "DuelGuild";
    MyQuests.QuestObjectives.Duel:OnCreateOrEdit(refStr, objOut);

    objOut.minLevel = tonumber(getglobal(refStr .. "MinLevel"):GetText());
    objOut.maxLevel = tonumber(getglobal(refStr .. "MaxLevel"):GetText());

    return 1;
  end

  return nil;
end
function MyQuests.QuestObjectives.DuelGuild:OnUpdate(refObj, targetInfo)
  if (targetInfo.guild == refObj.name) then
    MyQuests.QuestObjectives["DuelAny"]:OnUpdate(refObj, targetInfo);
  end
end
function MyQuests.QuestObjectives.DuelGuild:GetLeaderBoard(objective)
  return MyQuests.QuestObjectives:GetLeaderBoard(objective, MQL["DuelGuildLeaderBoard"]);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.DuelPlayer = {};
function MyQuests.QuestObjectives.DuelPlayer:OnCreateOrEdit(refStr, objOut)
  --objOut = {};

  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "DuelPlayer";
    MyQuests.QuestObjectives.Duel:OnCreateOrEdit(refStr, objOut);

    objOut.minLevel = tonumber(getglobal(refStr .. "MinLevel"):GetText());
    objOut.maxLevel = tonumber(getglobal(refStr .. "MaxLevel"):GetText());

   return 1;
  end

  return nil;
end
function MyQuests.QuestObjectives.DuelPlayer:OnUpdate(refObj, targetInfo)
  if (targetInfo.name == refObj.name) then
    MyQuests.QuestObjectives["DuelAny"]:OnUpdate(refObj, targetInfo);
  end
end
function MyQuests.QuestObjectives.DuelPlayer:GetLeaderBoard(objective)
  return MyQuests.QuestObjectives:GetLeaderBoard(objective, MQL["DuelPlayerLeaderBoard"]);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.DuelRace = {};
function MyQuests.QuestObjectives.DuelRace:OnCreateOrEdit(refStr, objOut)
  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "DuelRace";
    MyQuests.QuestObjectives.Duel:OnCreateOrEdit(refStr, objOut);

    objOut.minLevel = tonumber(getglobal(refStr .. "MinLevel"):GetText());
    objOut.maxLevel = tonumber(getglobal(refStr .. "MaxLevel"):GetText());

   return 1;
  end

  return nil;
end
function MyQuests.QuestObjectives.DuelRace:OnUpdate(refObj, targetInfo)
  if (targetInfo.race == refObj.name) then
    MyQuests.QuestObjectives["DuelAny"]:OnUpdate(refObj, targetInfo);
  end
end
function MyQuests.QuestObjectives.DuelRace:GetLeaderBoard(objective)
  return MyQuests.QuestObjectives:GetLeaderBoard(objective, MQL["DuelRaceLeaderBoard"]);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.KillMonster = {};
function MyQuests.QuestObjectives.KillMonster:OnCreateOrEdit(refStr, objOut)
  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "KillMonster";

    objOut.location = getglobal(refStr .. "LocationEntry"):GetText();
    if (objOut.location ~= nil and objOut.location ~= "") then
      objOut.coordX = tonumber(getglobal(refStr .. "XCoord"):GetText());
      objOut.coordY = tonumber(getglobal(refStr .. "YCoord"):GetText());
    end

    objOut.total = tonumber(getglobal(refStr .. "CountEntry"):GetText());
    if (not objOut.total or objOut.total == 0) then
      objOut.total = 1;
    end
    objOut.count = 0;
    
    return 1;
  end
  
  return nil;
end
function MyQuests.QuestObjectives.KillMonster:OnUpdate(refObj)
  if (refObj.count < refObj.total) then
    refObj.count = refObj.count + 1;
    if (refObj.count >= refObj.total) then
      refObj.isComplete = 1;
    end
    -- TODO: print out a progress
    --mq.IO.banner(mq.QuestObjectives.GetLeaderBoard(objective, MQ_MSG_QUESTUPDATE_MONSTER));
  end
end
function MyQuests.QuestObjectives.KillMonster:GetLeaderBoard(objective)
  return MyQuests.QuestObjectives:GetLeaderBoard(objective, MQL["KillMonsterLeaderBoard"]);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.Pvp = {};
function MyQuests.QuestObjectives.Pvp:OnCreateOrEdit(refStr, objOut)
  objOut.leaderBoard = getglobal(refStr .. "ShowLeader"):GetChecked();

  objOut.location = getglobal(refStr .. "LocationEntry"):GetText();
  if (objOut.location ~= nil and objOut.location ~= "") then
    objOut.coordX = tonumber(getglobal(refStr .. "XCoord"):GetText());
    objOut.coordY = tonumber(getglobal(refStr .. "YCoord"):GetText());
  end

  objOut.total = tonumber(getglobal(refStr .. "CountEntry"):GetText());
  if (not objOut.total or objOut.total == 0) then
    objOut.total = 1;
  end
  objOut.count = 0;
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.PvpAny = {};
function MyQuests.QuestObjectives.PvpAny:OnCreateOrEdit(refStr, objOut)
  objOut.type = "PvpAny";
  MyQuests.QuestObjectives.Pvp:OnCreateOrEdit(refStr, objOut);
  
  return 1;
end
function MyQuests.QuestObjectives.PvpAny:OnUpdate(refObj)
  MyQuests.QuestObjectives["DuelAny"]:OnUpdate(refObj);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.PvpClass = {};
function MyQuests.QuestObjectives.PvpClass:OnCreateOrEdit(refStr, objOut)
  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "PvpClass";
    MyQuests.QuestObjectives.Pvp:OnCreateOrEdit(refStr, objOut);
    
    return 1;
  end
  
  return nil;
end
function MyQuests.QuestObjectives.PvpClass:OnUpdate(refObj)
  MyQuests.QuestObjectives["DuelClass"]:OnUpdate(refObj);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.PvpGuild = {};
function MyQuests.QuestObjectives.PvpGuild:OnCreateOrEdit(refStr, objOut)
  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "PvpGuild";
    MyQuests.QuestObjectives.Pvp:OnCreateOrEdit(refStr, objOut);
    
    return 1;
  end
  
  return nil;
end
function MyQuests.QuestObjectives.PvpGuild:OnUpdate(refObj)
  MyQuests.QuestObjectives["DuelGuild"]:OnUpdate(refObj);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.PvpPlayer = {};
function MyQuests.QuestObjectives.PvpPlayer:OnCreateOrEdit(refStr, objOut)
  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "PvpPlayer";
    MyQuests.QuestObjectives.Pvp:OnCreateOrEdit(refStr, objOut);
    
    return 1;
  end
  
  return nil;
end
function MyQuests.QuestObjectives.PvpPlayer:OnUpdate(refObj)
  MyQuests.QuestObjectives["DuelPlayer"]:OnUpdate(refObj);
end

--[[ ********************************************************************* --]]
--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.PvpRace = {};
function MyQuests.QuestObjectives.PvpRace:OnCreateOrEdit(refStr, objOut)
  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "PvpRace";
    MyQuests.QuestObjectives.Pvp:OnCreateOrEdit(refStr, objOut);
    
    return 1;
  end
  
  return nil;
end
function MyQuests.QuestObjectives.PvpRace:OnUpdate(refObj)
  MyQuests.QuestObjectives["DuelRace"]:OnUpdate(refObj);
end

--[[ ********************************************************************* --]]
MyQuests.QuestObjectives.Tame = {};
function MyQuests.QuestObjectives.Tame:OnCreateOrEdit(refStr, objOut)
  objOut.name = getglobal(refStr .. "NameEntry"):GetText();
  if (objOut.name ~= nil and objOut.name ~= "") then
    objOut.type = "Tame";

    objOut.leaderBoard = getglobal(refStr .. "ShowLeader"):GetChecked();

    objOut.location = getglobal(refStr .. "LocationEntry"):GetText();
    if (objOut.location ~= nil and objOut.location ~= "") then
      objOut.coordX = tonumber(getglobal(refStr .. "XCoord"):GetText());
      objOut.coordY = tonumber(getglobal(refStr .. "YCoord"):GetText());
    end
    
    return 1;
  end
  
  return nil;
end
